<?php

namespace App\Http\Middleware;

use App\Service\Redis\RedisBaseService;
use Carbon\Carbon;
use Closure;
use Illuminate\Http\Request;

class checkUserStatus
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|object
     */
    public function handle(Request $request, Closure $next)
    {
        if (\Auth::guard('api')->check()) {
            $user = \Auth::guard('api')->user();
            if ($user) {
                if ($user->status != 'normal') {
                    return response()->json([
                        'success' => false,
                        'code' => 401,
                        'message' => '防盗刷机制，请联系客服！',
                        'timestamp' => Carbon::now()->timestamp
                    ]);
                }
            }
        }
        return $next($request);
    }
}
